package br.com.wryel.model.impl;
import java.io.Serializable;

import br.com.wryel.dao.DAOException;
import br.com.wryel.dao.DAOFactory;
import br.com.wryel.dao.GenericDAO;
import br.com.wryel.model.GenericModel;
import br.com.wryel.model.ModelException;


public abstract class GenericModelImpl<Entity extends Serializable, DAO extends GenericDAO<Entity>> implements GenericModel<Entity> {
	
	protected DAO dao;
	
	public GenericModelImpl(Class<DAO> daoClass) {
		dao = DAOFactory.getDAOByInterface(daoClass);
	}
	
	public void save(Entity entity) throws ModelException {
		try {
			dao.save(entity);
		} catch (DAOException daoException) {
			throw new ModelException(daoException.getMessage(), daoException);
		}
	}

	public Entity retrieve(Entity entity) throws ModelException {
		try {
			Entity entityFinded = dao.retrieve(entity);
			return entityFinded;
		} catch (DAOException daoException) {
			throw new ModelException(daoException.getMessage(), daoException);
		}
	}

	public void delete(Entity entity) throws ModelException {
		try {
			dao.delete(entity);
		} catch (DAOException daoException) {
			throw new ModelException(daoException.getMessage(), daoException);
		}
	}
}
